package auctionhaus



import org.junit.*

import grails.test.mixin.*

@TestFor(BidController)
@Mock(Bid)
class BidControllerTests {

	int unique = 0;

	Listing listing;
	Customer bidder;


	@Before
	void beforeTest(){

		mockDomain(Customer)

		Customer seller = new Customer()
		seller.username="mark"+ unique +"@live.com"
		seller.password="132242"
		def springSecurityService = new Object()
		springSecurityService.metaClass.encodePassword = {
			String password -> "ENCODED_PASSWORD"
		}
		seller.springSecurityService = springSecurityService


		assertNotNull(seller.save(failOnError:true, flush:true))

		bidder = new Customer()
		bidder.username="bushrat"+ unique +"@live.com"
		bidder.password="132242"
		bidder.springSecurityService = springSecurityService

		assertNotNull(bidder.save(failOnError:true, flush:true))

		unique++;

		mockDomain(Listing)
		listing = new Listing();
		listing.name = "My Listing"
		listing.endDateTime = new Date()+7
		listing.seller = seller
		listing.startingPrice = 12.00

		assertNotNull(listing.save(failOnError:true, flush:true))
	}

	/*
	 * UI-4: The action of placing a new bid will display a message to the user indicating that the bid was successful (Controller Unit Test)
	 */
	void testSaveBidAndReturnDetailsAjaxValid(){
		controller.crudService = new CrudService();
		controller.save()


		response.reset()

		params["bidder.id"] = bidder.id
		params["listing.id"] = listing.id
		params["amount"] = 14.00

		controller.saveBidAndReturnDetailsAjax()

		assertEquals('default.created.message',flash.message)

	}
	
	/*
	 * UI-5: An error message will be displayed if placing a new bid is unsuccessful (for instance if the new bid amount does not pass validation requirements) (Controller Unit Test)
	 */
	void testSaveBidAndReturnDetailsAjaxInvalid(){
		controller.crudService = new CrudService();
		controller.save()


		response.reset()

		params["bidder.id"] = bidder.id
		params["listing.id"] = listing.id
		params["amount"] = 8.00

		controller.saveBidAndReturnDetailsAjax()

		assertEquals('bids.invalid.amount',flash.message)

	}
}
